package com.fulllife.food.foodstrategy;

import com.fulllife.food.entity.FoodEntity;
import com.fulllife.food.enums.FoodTypeEnum;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 检查是否荤素搭配。如果缺肉，提示补充肉。如果缺少素菜，提示补充素菜。
 */
@Service
@Slf4j
public class MeatStrategy implements FoodStrategy {


    @Override
    public Mono<Analyse> analyze(Flux<FoodEntity> foodEntities) {

        Flux<List<FoodTypeEnum>> buffer = foodEntities.groupBy(FoodEntity::getFoodType)
            .map(flux -> FoodTypeEnum.valueOf(flux.key())).buffer();

        return buffer.map(part -> {
            final List<FoodTypeEnum> collect = Arrays.stream(FoodTypeEnum.values()).collect(Collectors.toList());
            collect.removeAll(part);

            return Analyse.builder().contains(part).suggestion(collect).build();
        }).collectList().map(list -> list.get(0));

    }
}
